package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

//        向套餐表插入数据
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insertSetmeal(setmeal);

//        获取生成的套餐id
        Long id = setmeal.getId();

        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        dishes.forEach(dish -> {
            dish.setSetmealId(id);
        });

//        保存套餐和菜品的关联关系
        setmealDishMapper.insertDishBySetmeal(dishes);
    }

    /**
     * 分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<SetmealVO> setmeals = setmealMapper.pageQuery(pageQueryDTO);

//        判断是否找到符合条件的内容
        if (setmeals == null || setmeals.size() == 0) {
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_NOT_FOUND);
        }

//        判断包含套餐的分类是否被禁用

        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }

    /**
     * 起售或停售套餐
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
//        判断套餐内是否包含未起售菜品
        if (status == StatusConstant.ENABLE) {
            List<Dish> setmealDishes = dishMapper.getBySetmeal(id);
            setmealDishes.forEach(dish -> {
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.updateSetmeal(setmeal);
    }


    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
//        判断被删除套餐是否处于起售状态
        List<Setmeal> setmeals = setmealMapper.getByIds(ids);
        setmeals.forEach(setmeal -> {
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

//        删除套餐及套餐菜品表中的关联
        ids.forEach(id -> {
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteBySetmealId(id);
        });
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
//        查询套餐信息
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

//        查询与菜品的关联
        List<SetmealDish> dishes = setmealDishMapper.getDishBySetmealId(id);

        setmealVO.setSetmealDishes(dishes);

        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

//        判断是否包含停售菜品
//        if (setmeal.getStatus() == StatusConstant.ENABLE){
//            setmealDishes.forEach(dish -> {
//                if (dish.getStatus() == StatusConstant.DISABLE){
//                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_UPDATE_FAILED);
//                }
//            });
//        }

        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        dishes.forEach(dish -> {
//            删除原有的菜品与套餐的联系
            setmealDishMapper.deleteBySetmealId(setmeal.getId());
//            添加新的菜品与套餐的联系
            dish.setSetmealId(setmeal.getId());
        });

//        插入联系
        setmealMapper.updateSetmeal(setmeal);
        setmealDishMapper.insertDishBySetmeal(dishes);

    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.list(categoryId);
        return setmeals;
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getSetmealDishes(Long id) {
        List<DishItemVO> setmealDishes = setmealMapper.getSetmealDishes(id);
        return setmealDishes;
    }
}
